package ar.uade.distribuidas.tpo.web.servlets;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.w3c.dom.Document;

import ar.uade.distribuidas.tpo.base.dominiodto.RemitoDTO;
import ar.uade.distribuidas.tpo.dao.RemitoXmlDAO;
import ar.uade.distribuidas.tpo.servicios.MercaderiaService;
import ar.uade.distribuidas.tpo.servicios.MercaderiaServiceImpl;
import ar.uade.distribuidas.tpo.web.delegado.BD;


/**
 * Servlet implementation class EnviarMercaderia
 */
@WebServlet("/EnviarMercaderia")
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
maxFileSize=1024*1024*10,      // 10MB
maxRequestSize=1024*1024*50)   // 50MB
public class EnviarMercaderia extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EnviarMercaderia() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/jsp/enviarmerca.jsp");
		Part parteArch = request.getPart("file");
		 InputStream filecontent = parteArch.getInputStream();
		
		
				
		
		
		try{

			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder;
			builder = factory.newDocumentBuilder();
			Document doc = builder.parse(filecontent);
			MercaderiaService service = new MercaderiaServiceImpl();
			Set<RemitoDTO> remitosXML = service.procesarXMLMercaderia(doc);
			BD.getInstance().enviarMercaderia(remitosXML);
			String remitosResult ="";
			
			for(RemitoDTO remito : remitosXML){
				remitosResult += RemitoXmlDAO.remitoToXmlString(remito);	
			}
			
		
		request.setAttribute("xmlgen1", remitosResult);

		request.setAttribute("allok", "Los remitos fueron procesada correctamente.");
		
		 request.getRequestDispatcher("/jsp/enviarmerca.jsp").forward(request, response);
		//dispatcher.forward(request, response);	
		} catch (Exception e) {
			//dispatcher = request.getRequestDispatcher("WEB-INF/jsp/allerror.jsp");
			request.setAttribute("allerror", "Hubo un error al procesar la solicitud.");
			request.getRequestDispatcher("/jsp/enviarmerca.jsp").forward(request, response);
			
			
			//dispatcher.forward(request, response);
		}
		
		//request.setAttribute("xmlgen", request.getParameter("file"));
		//request.setAttribute("cuil", "hola");
		
		
	}

}
